//
//  MyOrderVC.m
//  同盟圈
//
//  Created by 洛水寒 on 16/4/19.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "MyOrderVC.h"
#import "OrderListVC.h"
#import "MBProgressHUD.h"
@interface MyOrderVC ()<KZTabBarDelegate, SlideSwitchViewDelegate>
{
    NSString *_userId;
    MyOrderTabBar *_tabBar;
    BOOL flagRef;
    SlideSwitchView *_slideSwitchView;
    NSInteger index;
}

@end

@implementation MyOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的订单";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tabBar = [[MyOrderTabBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _tabBar.backgroundColor = [UIColor whiteColor];

    _tabBar.delegate = self;
    [self.view addSubview:_tabBar];

    _slideSwitchView = [[SlideSwitchView alloc] initWithFrame:CGRectMake(0, 48, SCREEN_WIDTH, self.view.frame.size.height-45)];
    _slideSwitchView.backgroundColor = [UIColor clearColor];
    _slideSwitchView.delegate = self;
    [self.view addSubview:_slideSwitchView];
    [self initialize];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CancelDingDan:) name:@"CancelDingDan" object:nil];
}

- (void)CancelDingDan:(NSNotification *)noti {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(noti.userInfo[@"Message"], @"HUD message title");
    hud.offset = CGPointMake(0.f, 80.f);
    
    [hud hideAnimated:YES afterDelay:1.f];
}

// 返回
- (void)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
//    NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, [UIColor whiteColor],NSForegroundColorAttributeName  ,nil];;
//    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *flag = [userDefaults objectForKey:@"refreshOrderList"];
    
    if ([flag isEqualToString:@"1"]) {
        flagRef = YES;
        index = _tabBar.selectedIndex;
        [_tabBar removeFromSuperview];
        [self viewDidLoad];
        [userDefaults setObject:@"0" forKey:@"refreshOrderList"];
        [userDefaults synchronize];
    }else{
        flagRef = NO;
    }
    
}

- (void)initialize
{
    
    
    if (self.viewControllers == nil) {
        self.viewControllers = [NSMutableArray array];
    }
    [self.viewControllers removeAllObjects];
    NSArray *titleArr = @[@"全部",@"待付款", @"已付款"];
    NSArray *statusArr = @[@"0", @"1", @"2"];
    
    for (int i = 0; i < [titleArr count]; i++) {
        int orderStatus = [statusArr[i] intValue];

        OrderListVC *viewController = [[OrderListVC alloc] initWithUserId:_userId orderStatus:orderStatus];
        viewController.rootViewController = self;
        [self.viewControllers addObject:viewController];
        //
        
    }
    NSLog(@"------------点击了%d",_orderStatus);
     [_slideSwitchView setSelectedIndex:_orderStatus];
    _tabBar.titles = titleArr;
    
    if (flagRef) {
        _tabBar.selectedIndex = index; 
        
        _slideSwitchView.selectedIndex = index;
        flagRef = NO;
    }else{
        _tabBar.selectedIndex = 0;
    }
    
    [_slideSwitchView performSelector:@selector(reloadData) withObject:nil afterDelay:0];
}
#pragma mark - KZTabBarDelegate

- (void)tabBar:(KZTabBar *)tabBar didSelectItem:(KZTabBarItem *)item
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTableView" object:nil userInfo:nil];
    
    [_slideSwitchView setSelectedIndex:_tabBar.selectedIndex];
}

#pragma mark - SlideSwitchViewDelegate

- (NSUInteger)numberOfTabs:(SlideSwitchView *)view
{
    return [_tabBar numberOfItems];
}

- (UIViewController *)slideSwitchView:(SlideSwitchView *)view viewOfTab:(NSUInteger)number
{
    if (number < [self.viewControllers count]) {
        return self.viewControllers[number];
    }
    return nil;
}

- (void)slideSwitchView:(SlideSwitchView *)view panLeftEdge:(UIPanGestureRecognizer *)panGesture { }

- (void)slideSwitchView:(SlideSwitchView *)view panRightEdge:(UIPanGestureRecognizer *)panGesture { }

- (void)slideSwitchView:(SlideSwitchView *)view didSelectTab:(NSUInteger)number
{
    if (number < [self.viewControllers count]) {
        [_tabBar setSelectedIndex:number];
    }
}


@end
